﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.WinControls.UI;
using LIM.model;
using LIM.controller;
using Telerik.WinControls.Data;
using p_Library;

namespace LIM.forms.uc_CRM_PhieuNhanMau_ChiTiet
{
    public partial class uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem : UserControl
    {
        radForm_CRM_PhieuNhanMau_ChiTiet parentForm = null;

        ColumnGroupsViewDefinition columnGroupsView_Mau;
        ColumnGroupsViewDefinition columnGroupsView_ChiTieu;

        DanhMucChiTieuController DanhMucChiTieuController = new DanhMucChiTieuController();

        //List<tbl_CRM_PhieuNhanMau_MauDuocNhan> tbl_CRM_PhieuNhanMau_MauDuocNhans = new List<tbl_CRM_PhieuNhanMau_MauDuocNhan>();
        
        public uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem()
        {
            InitializeComponent();

            InitializeGrid();
        }

        private void InitializeGrid()
        {            
            this.columnGroupsView_Mau = new ColumnGroupsViewDefinition();
            this.columnGroupsView_Mau.ColumnGroups.Add(new GridViewColumnGroup());
            this.columnGroupsView_Mau.ColumnGroups.Add(new GridViewColumnGroup());
            this.columnGroupsView_Mau.ColumnGroups[0].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView_Mau.ColumnGroups[0].ShowHeader = false;
            this.columnGroupsView_Mau.ColumnGroups[1].ShowHeader = false;
            this.columnGroupsView_Mau.ColumnGroups[0].Rows[0].Columns.Add(this.radGridView_DanhSachMau.Columns["SoThuTu"]);
            this.columnGroupsView_Mau.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView_Mau.ColumnGroups[1].Rows.Add(new GridViewColumnGroupRow());
            this.columnGroupsView_Mau.ColumnGroups[1].Rows[0].Columns.Add(this.radGridView_DanhSachMau.Columns["TenMau"]);
            this.columnGroupsView_Mau.ColumnGroups[1].Rows[1].Columns.Add(this.radGridView_DanhSachMau.Columns["TinhTrangMau"]);
            this.columnGroupsView_Mau.ColumnGroups[1].Rows[1].Columns.Add(this.radGridView_DanhSachMau.Columns["MaSoMau"]);
            this.radGridView_DanhSachMau.ViewDefinition = columnGroupsView_Mau;

        }

        void parentForm_OnLuuLai_Click()
        {
            if (parentForm.tbl_CRM_PhieuNhanMau != null)
            {
                parentForm.tbl_CRM_PhieuNhanMau.SoTang = this.radTextBox_SoTang.Text;
                parentForm.tbl_CRM_PhieuNhanMau.SoGiam = this.radTextBox_SoGiam.Text;
                parentForm.tbl_CRM_PhieuNhanMau.VAT = int.Parse(this.radTextBox_VAT.Text);
                parentForm.tbl_CRM_PhieuNhanMau.NgoaiTeChuyenDoi = this.radDropDownList_KieuNgoaiTe.SelectedItem.Text;
                parentForm.tbl_CRM_PhieuNhanMau.NgoaiTeChuyenDoi_GiaTri = decimal.Parse(this.radTextBox_GiaTriNgoaiTe.Text.Trim());
            }
        }

        private void uc_CRM_PhieuNhanMau_ChiTiet_ChiTieuKiemNghiem_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_CRM_PhieuNhanMau_ChiTiet)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnLuuLai_Click += new radForm_CRM_PhieuNhanMau_ChiTiet.LuuLai_Click(parentForm_OnLuuLai_Click);
            }    

            DataLoad();            
        }

        private void DataLoad()
        {
            this.radDropDownList_KieuNgoaiTe.SelectedIndex = 0;

            if (parentForm.tbl_CRM_PhieuNhanMau != null)
            {
                List<tbl_DanhMuc_ChiTieu> list_ChiTieu = parentForm.CRM_PhieuNhanMauController.LoadChiTieu();
                GridViewMultiComboBoxColumn colMaChiTieu = this.radGridView_DanhSachChiTieu.Columns["MaChiTieu"] as GridViewMultiComboBoxColumn;
                GridViewMultiComboBoxColumn colTenChiTieu = this.radGridView_DanhSachChiTieu.Columns["TenGoi"] as GridViewMultiComboBoxColumn;
                colMaChiTieu.DataSource = null;
                colTenChiTieu.DataSource = null;
                if (list_ChiTieu.Count > 0)
                {
                    colMaChiTieu.DataSource = list_ChiTieu;
                    colTenChiTieu.DataSource = list_ChiTieu;
                }                

                if (parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan == null)
                {
                    parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan = new System.Data.Objects.DataClasses.EntityCollection<tbl_CRM_PhieuNhanMau_MauDuocNhan>();
                }
                this.radGridView_DanhSachMau.DataSource = parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan;
            }

            TinhToanChiPhi();
        }

        private void radGridView_DanhSachMau_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
        {            
            e.Rows[0].Cells[0].Value = this.radGridView_DanhSachMau.RowCount + 1;            
        }
        private void radGridView_DanhSachMau_UserAddedRow(object sender, GridViewRowEventArgs e)
        {            
        }

        private void radGridView_DanhSachMau_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            if (e.CurrentRow is GridViewNewRowInfo)
                return;

            tbl_CRM_PhieuNhanMau_MauDuocNhan tbl_CRM_PhieuNhanMau_MauDuocNhan = (tbl_CRM_PhieuNhanMau_MauDuocNhan)e.CurrentRow.DataBoundItem;
            if (tbl_CRM_PhieuNhanMau_MauDuocNhan != null)
            {
                if (tbl_CRM_PhieuNhanMau_MauDuocNhan.tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem == null)
                {
                    tbl_CRM_PhieuNhanMau_MauDuocNhan.tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem = new System.Data.Objects.DataClasses.EntityCollection<tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem>();
                }    
                this.radGridView_DanhSachChiTieu.DataSource = tbl_CRM_PhieuNhanMau_MauDuocNhan.tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem;
            }
        }

        private void radGridView_DanhSachChiTieu_UserAddingRow(object sender, GridViewRowCancelEventArgs e)
        {
            if (this.radGridView_DanhSachMau.CurrentRow is GridViewNewRowInfo)
            {
                e.Cancel = true;
                return;
            }
            e.Rows[0].Cells[0].Value = this.radGridView_DanhSachChiTieu.RowCount + 1;

            TinhToanChiPhi();
        }
        private void radGridView_DanhSachChiTieu_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem = e.Row.DataBoundItem as tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem;
            //tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem.SoThuTu = this.radGridView_DanhSachChiTieu.RowCount;
            tbl_DanhMuc_ChiTieu tbl_DanhMuc_ChiTieu = parentForm.CRM_PhieuNhanMauController.LoadChiTieu_ByRowID(tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem.RowIDChiTieu);
            tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem.RowIDChiTieu = 0;
            tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem.tbl_DanhMuc_ChiTieu = tbl_DanhMuc_ChiTieu;
            tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem.tbl_DanhMuc_ChiTieuReference.Load();

            TinhToanChiPhi();
        }

        private void radGridView_DanhSachChiTieu_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            if (this.radGridView_DanhSachChiTieu.CurrentColumn is GridViewMultiComboBoxColumn)
            {
                Create_GridViewMultiComboBoxColumn();
            }
            else if (this.radGridView_DanhSachChiTieu.CurrentColumn is GridViewDecimalColumn)
            {
                GridSpinEditor GridSpinEditor = (GridSpinEditor)this.radGridView_DanhSachChiTieu.ActiveEditor;
                GridSpinEditor.ThousandsSeparator = true;

            }
        }
        RadMultiColumnComboBoxElement editor = null;
        private void Create_GridViewMultiComboBoxColumn()
        {
            if (editor == null)
            {
                editor = (RadMultiColumnComboBoxElement)this.radGridView_DanhSachChiTieu.ActiveEditor;
                editor.AutoSizeDropDownToBestFit = true;
                editor.EditorControl.AutoGenerateColumns = false;

                List<string> columnNames= new List<string>{"MaChiTieu","TenGoi", "tbl_DanhMuc_LoaiMau"};
                foreach (var item in editor.EditorControl.Columns)
                {
                    if (!columnNames.Contains(item.Name))
                        item.IsVisible = false;
                }
                editor.EditorControl.Columns["tbl_DanhMuc_LoaiMau"].FieldName = "tbl_DanhMuc_LoaiMau.TenGoi";
                editor.EditorControl.Columns["tbl_DanhMuc_LoaiMau"].HeaderText = "Loại mẫu";

                //Filter MaChiTieu
                FilterDescriptor oFilterDescriptorMaChiTieu = new FilterDescriptor("MaChiTieu", FilterOperator.Contains, "");
                oFilterDescriptorMaChiTieu.IsFilterEditor = true;
                editor.EditorControl.Columns["MaChiTieu"].FilterDescriptor = oFilterDescriptorMaChiTieu;
                // Filter TenChiTieu
                FilterDescriptor oFilterDescriptorTenChiTieu = new FilterDescriptor("TenGoi", FilterOperator.Contains, "");
                oFilterDescriptorTenChiTieu.IsFilterEditor = true;
                editor.EditorControl.Columns["TenGoi"].FilterDescriptor = oFilterDescriptorTenChiTieu;
                
                editor.EditorControl.FilterDescriptors.LogicalOperator = FilterLogicalOperator.Or;
                editor.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                editor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
                editor.EditorControl.ShowFilteringRow = false;
                editor.AutoFilter = true;
                editor.SelectedIndex = -1;

                editor.SelectedValueChanged += new EventHandler(editor_SelectedValueChanged);
            }
        }
        void editor_SelectedValueChanged(object sender, EventArgs e)
        {
            GridViewDataRowInfo GridViewDataRowInfo = editor.SelectedItem as GridViewDataRowInfo;
            if (GridViewDataRowInfo != null)
            {
                tbl_DanhMuc_ChiTieu tbl_DanhMuc_ChiTieu = GridViewDataRowInfo.DataBoundItem as tbl_DanhMuc_ChiTieu;

                if (tbl_DanhMuc_ChiTieu != null)
                {
                    if (this.radGridView_DanhSachChiTieu.CurrentColumn.Name == "MaChiTieu")
                    {
                        this.radGridView_DanhSachChiTieu.CurrentRow.Cells["TenGoi"].Value = tbl_DanhMuc_ChiTieu.RowID;
                    }
                    else if (this.radGridView_DanhSachChiTieu.CurrentColumn.Name == "TenGoi")
                    {
                        this.radGridView_DanhSachChiTieu.CurrentRow.Cells["MaChiTieu"].Value = tbl_DanhMuc_ChiTieu.RowID;
                    }

                    this.radGridView_DanhSachChiTieu.CurrentRow.Cells["PhuongPhap"].Value = tbl_DanhMuc_ChiTieu.PhuongPhap;
                    this.radGridView_DanhSachChiTieu.CurrentRow.Cells["SoLuong"].Value = 1;

                    tbl_DanhMuc_ChiTieu_BangGia tbl_DanhMuc_ChiTieu_BangGia = DanhMucChiTieuController.LoadGiaChiTieu(tbl_DanhMuc_ChiTieu);
                    if (tbl_DanhMuc_ChiTieu_BangGia != null)
                        this.radGridView_DanhSachChiTieu.CurrentRow.Cells["DonGia"].Value = tbl_DanhMuc_ChiTieu_BangGia.Gia.GetValueOrDefault(0);
                    else
                        this.radGridView_DanhSachChiTieu.CurrentRow.Cells["DonGia"].Value = 0;

                    if (this.radGridView_DanhSachChiTieu.CurrentRow is GridViewNewRowInfo)
                        this.radGridView_DanhSachChiTieu.CurrentRow.Cells["SoThuTu"].Value = this.radGridView_DanhSachChiTieu.RowCount + 1;
                }
            }
        }

        private void radGridView_DanhSachChiTieu_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            if (this.radGridView_DanhSachChiTieu.CurrentColumn is GridViewDecimalColumn)
            {
                TinhToanChiPhi();
            }
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

        private void TinhToanChiPhi()
        {
            decimal TongTheoChiTieu = 0;
            string SoTang = "0";
            decimal SoTang_GiaTri = 0;
            string SoGiam = "0";
            decimal SoGiam_GiaTri = 0;

            decimal ChiPhi = 0;

            foreach (var item in parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan)
            {
                foreach (var item2 in item.tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem)
                {
                    TongTheoChiTieu += item2.SoLuong.GetValueOrDefault(0) * item2.DonGia.GetValueOrDefault(0);
                }
            }
            this.radTextBox_TongTheoChiTieu.Text = cls_ReadNumber.mtd_ChangeNumber(TongTheoChiTieu.ToString(), 0);

            ChiPhi = TongTheoChiTieu;

            if (decimal.TryParse(this.radTextBox_SoTang.Text.Replace(",","").Trim(), out SoTang_GiaTri))
            {
                ChiPhi += SoTang_GiaTri;
            }
            else
            {
                if (decimal.TryParse(this.radTextBox_SoTang.Text.Replace("%", "").Trim(), out SoTang_GiaTri))
                {
                    ChiPhi += TongTheoChiTieu * SoTang_GiaTri / 100;
                }
            }

            if (decimal.TryParse(this.radTextBox_SoGiam.Text.Replace(",", "").Trim(), out SoGiam_GiaTri))
            {
                ChiPhi -= SoGiam_GiaTri;
            }
            else
            {
                if (decimal.TryParse(this.radTextBox_SoGiam.Text.Replace("%", "").Trim(), out SoGiam_GiaTri))
                {
                    ChiPhi -= TongTheoChiTieu * SoGiam_GiaTri / 100;
                }
            }

            this.radTextBox_ChiPhiKiemNghiem.Text = cls_ReadNumber.mtd_ChangeNumber(ChiPhi.ToString(), 0);
        }

        private void radTextBox_SoGiam_TextChanged(object sender, EventArgs e)
        {            
            TinhToanChiPhi();
        }

        private void radTextBox_SoTang_TextChanged(object sender, EventArgs e)
        {
            
            TinhToanChiPhi();
        }
    }

    public class CustomGridNewRowElement_PhieuNhanMau : GridNewRowElement
    {
        //Prevent themes from getting all messed up
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(GridNewRowElement);
            }
        }

        public override void UpdateContentVisibility(bool showCells)
        {
            base.UpdateContentVisibility(showCells);

            if (this.Text != string.Empty)
            {
                if (this.RowInfo.HierarchyLevel == 0)
                {
                    this.Text = "Nhập mẫu nhận mới tại đây";
                }
                else if (this.RowInfo.HierarchyLevel == 2)
                {
                    this.Text = "Nhập chỉ tiêu kiểm nghiệm tại đây";
                }
            }
        }
    }

}
